#!/bin/bash
source /etc/profile
. $BEETLE_SHELL_PATH/error.sh

projectName=$1

if [ ! ${projectName} ]
then
    error_exit "参数存在空值projectName"
fi

. /usr/local/python_vm/${projectName}/bin/activate || error_exit "启动虚拟环境失败"

# source /etc/profile
# . $BEETLE_SHELL_PATH/error.sh

# 分支名
branch=$2


codePath=/usr/local/beetle/code/${projectName}
workPath=/usr/local/python
configPath=/usr/local/beetle/config/${projectName}
bakPath=/usr/beetle/appbak/${projectName}
bakName=${projectName}
logPath=/usr/beetle/applogs/${projectName}/${projectName}.log
iniPath=$BEETLE_UWSGI_PATH/${projectName}.ini

bash $BEETLE_SHELL_PATH/git_checkout.sh ${codePath} ${branch} || error_exit

bash $BEETLE_SHELL_PATH/bak.sh ${workPath} ${projectName} ${bakPath} ${bakName} || error_exit

cd ${codePath} || error_exit

pip install -r app/requirements.txt || error_exit

rm -rf ${workPath}/${projectName}/* || error_exit

cp -Rf `ls |grep -v "git"` ${workPath}/${projectName}/ || error_exit

cp ${configPath}/* ${workPath}/${projectName}/ || error_exit

pip install uwsgi

uwsgi.sh stop ${projectName}

uwsgi --http-websockets --memory-report --disable-logging -d ${logPath} --ini ${iniPath} || error_exit

tail -fn20 ${logPath}
